import {getAllNotice, getFrontendData, getPreviewNewsContent} from '@/api/frontend'

const state = {
    companyAndJobs: [],
    frontendNotice: [],
    frontendNoticeTotal: 0,
}

const mutations = {

    SET_COMPANYANDJOBS: (state, data) => {
        state.companyAndJobs = data
    },

    SET_FRONTENDNOTICE: (state, data) => {
        state.frontendNotice = data.records
        state.frontendNoticeTotal = data.total
    }
}

const actions = {

    /**
     * 得到首页数据
     * @param commit
     * @returns {Promise<unknown>}
     */
    getFrontendData({commit}) {
        return new Promise((resolve, reject) => {
            getFrontendData().then((resp) => {
                const { data } = resp.data
                if (data === undefined) reject('数据异常！')
                commit('SET_COMPANYANDJOBS', data)
                resolve(data)
            }).catch(error => reject(error.msg))
        })
    },

    getPreviewNewsContent({commit}, id) {
        return new Promise((resolve, reject) => {
            getPreviewNewsContent(id).then((resp) => {
                if (resp.data.code === 1) {
                    resolve(resp)
                }
                reject("error")
            }).catch((error) => {
                reject(error)
            })
        })
    },

    getAllNotice({ commit }, data) {
        return new Promise((resolve, reject) => {
            getAllNotice(data).then(
                resp => {
                    if (resp.data.code === 1) {
                        commit("SET_FRONTENDNOTICE", resp.data.data)
                        resolve(true)
                    } else if (resp.data.code === 0 ) {
                        reject(resp.data.msg)
                    }
                    reject("error")
                }).catch(
                error => {
                    reject(error)
                })
        })
    },

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
